package demo.example.demo.controller;

import demo.example.demo.DTO.LivingDetectInitRequest;
import demo.example.demo.DTO.RpVerifyRequest;
import demo.example.demo.base.gateway.AlgorithmE;
import demo.example.demo.base.gateway.ESGateway;
import demo.example.demo.base.gateway.entity.GatewayResponse;
import demo.example.demo.util.CommUtil;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@Controller
public class rpVerifyController {


    @RequestMapping("auth/index")
    public String indexTo() {
        return "fldt/qrcode_rpverify/index";
    }

    @PostMapping("auth/getVerifyUrl")
    @ResponseBody
    public String getUrl(RpVerifyRequest request) throws Exception {
        log.info("请求参数:{}", request);
        String appcode = "你的appcode";
        String key = "你的密钥";
        AlgorithmE algo = AlgorithmE.MD5;
        if(request.getIdName().isEmpty() || request.getIdNumber().isEmpty()){
            return "参数异常，姓名、身份证号不能为空";
        }
        request.setBizId(CommUtil.getTimeUUID());

        ESGateway esGateway = new ESGateway(appcode, algo, key);
        GatewayResponse gatewayResponse = esGateway.sendToGateWay(request.act, request.toJsonStr());
        log.info("服务器端返回：{}", gatewayResponse.toJsonStr());
        if (gatewayResponse == null || gatewayResponse.getCode().equals("-1") || StringUtil.isBlank(gatewayResponse.getBizContent())) {
            return "服务异常" + gatewayResponse.getCode() + gatewayResponse.getMsg();
        }
        return gatewayResponse.getBizContent();
    }
}
